<template>
  <video-player ref="videoPlayer" class="video-player vjs-custom-skin" :playsinline="true"
    :options="playerOptions" :events="events" @waiting="onPlayerWaiting($event)"
    @canplay="onPlayerCanplay($event)" v-loading="videoLoading" @error="onPlayerError"
    element-loading-background="rgba(0,0,0,0)">
  </video-player>
</template>
 
<script>
import videojs from "video.js";
import "video.js/dist/video-js.css";
import "vue-video-player/src/custom-theme.css";
import { videoPlayer } from "vue-video-player";
import "videojs-flash";
import "videojs-contrib-hls";
import SWF_URL from "videojs-swf/dist/video-js.swf";

videojs.options.flash.swf = SWF_URL; // 设置flash路径，Video.js会在不支持html5的浏览中使用flash播放视频文件
export default {
  name: "video-js",
  components: {
    videoPlayer
  },
  data() {
    return {
      videoLoading: true,
      events: ['playing'],
      currentRatio:'',
      playerOptions: {
        controls: true,
        bigPlayButton: false,
        live: true,
        autoplay: true, // 如果true，浏览器准备好时开始播放
        muted: false, // 默认情况下将会消除任何音频
        loop: false, // 是否视频一结束就重新开始
        preload: "auto", // 建议浏览器在<video>加载元素后是否应该开始下载视频数据。auto浏览器选择最佳行为,立即开始加载视频（如果浏览器支持）
        aspectRatio: "3:2", // 将播放器置于流畅模式，并在计算播放器的动态大小时使用该值。值应该代表一个比例 - 用冒号分隔的两个数字（例如"16:9"或"4:3"）
        fluid: true, // 当true时，Video.js player将拥有流体大小。换句话说，它将按比例缩放以适应其容器。
        controlBar: {
          timeDivider: false,
          durationDisplay: false,
          remainingTimeDisplay: false,
          currentTimeDisplay: false, // 当前时间
          volumePanel: {
            inline: false
          },
          playToggle: true, // 暂停和播放键
          progressControl: false, // 进度条
          fullscreenToggle: true // 全屏按钮
        },
        techOrder: ["flash", "html5"], // 兼容顺序
        flash: {
          hls: {
            withCredentials: false
          },
          swf: SWF_URL
        },
        html5: { hls: { withCredentials: false } },
        sources: [
          {
            type: "rtmp/flv",
            src: "" // 视频地址-改变它的值播放的视频会改变
          }
        ],
        notSupportedMessage: "此视频暂无法播放，请稍后再试" // 允许覆盖Video.js无法播放媒体源时显示的默认信息。
      }
    };
  },
  props: {
    reLoad: {
      type: Boolean,
      default: false
    },
    destroy:{
      type: Boolean,
      default: false
    }
  },
  computed: {
    player() {
      return this.$refs.videoPlayer.player;
    }
  },
  beforeDestroy() {
    this.player.dispose();
  },
  watch: {
    reLoad(val) {
      if(this.playerOptions.sources.src){
        // this.player.pause();
        this.player.src(this.playerOptions.sources.src);
        this.player.load();
        this.player.play();
      }  
    },
    destroy(val){
      if(val){
        this.player.dispose();
      }
    }
  },
  methods:{
    onPlayerWaiting($event){
      this.videoLoading = true;
    },
    onPlayerCanplay($event){
      this.videoLoading = false;
    },
    onPlayerError(){
      console.log("onPlayerError")
      this.videoLoading = false;
    }
  }
};
</script>
 
<style scoped lang="scss">
</style>
